package com.example.springweb.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.springweb.model.ApiResult;
import com.example.springweb.model.dto.ProductDTO;
import com.example.springweb.model.entity.Product;
import com.example.springweb.model.vo.ProductVo;
import com.example.springweb.service.IProductService;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "产品接口")
@RestController
@RequestMapping(value = "/product")
public class ProductController {
	
	@Autowired
	private IProductService productService;

	@Schema(name = "获取产品信息")
	@GetMapping("/detail/{id}")
	public ApiResult<ProductVo> getProduct(@PathVariable Long id){
		ProductVo vo = productService.getProductById(id);
		return ApiResult.success("请求成功，返回数据", vo);
	}
	
	@Schema(name = "新增产品信息")
	@PostMapping("/save")
	public ApiResult<String> save(@RequestBody Product product){
		int res = productService.insert(product);
		if(res > 0) {
			return ApiResult.success("产品信息保存成功");
		} else {
			return ApiResult.fail("产品信息保存失败");
		}
	}
	
	@Schema(name = "按类别查询")
	@PostMapping("/findByCategroy")
	public ApiResult<IPage<ProductVo>> findByCategory(@RequestBody ProductDTO dto){
		IPage<ProductVo> list = productService.findPruductByCategory(dto);
		return ApiResult.success(list);
	}
	
	@Schema(name = "列表")
	@PostMapping("/list")
	public ApiResult<List<Product>> list(@RequestBody ProductDTO product){
		List<Product> list = productService.selectList(product);
		return ApiResult.success(list);
	}
}
